<!--
 * @Author: WLang
 * @LastEditors: WLang
 * @Description: 
 * @Date: 2023-07-16 14:23:53
 * @LastEditTime: 2023-07-19 17:50:04
 * @FilePath: \myblog\src\mviews\UserView.vue
-->
<template>
  <div class="blogm-user">
    <div class="blogm-user--form">
      <van-uploader v-if="avatar" :after-read="afterRead">
        <van-image
          width="5rem"
          height="5rem"
          :src="avatar"
        />
      </van-uploader>
      <van-image v-else  width="5rem"
          height="5rem" round :src="require('@/assets/images/logo.png')"/>
    <div class="blogm-user--content">
      <h2 class="blogm-user--title">{{title}}</h2>
      <BaseForm ref="userForm" :type="formType"/>
      <p v-if="formType === 'login'" class="blogm-user--hint">首次使用？<van-tag type="primary" plain @click="gotoRegister">点我注册</van-tag></p>
      <p v-if="formType === 'register'" class="blogm-user--hint">已有账号？<van-tag type="primary" plain @click="gotoLogin">点我登录</van-tag></p>
    </div>
    <div class="blogm-user--btn">
      <van-button type="info" block @click="onSubmit">{{btnName}}</van-button>
      <van-button type="warning" block @click="logout" v-if="this.$store.state.token">退出登录</van-button>
    </div>
  </div>
  </div>
</template>

<script>
const BTN_NAME = {
  'login': '登录',
  'register': '注册',
  'userInfo': '修改'
}
import MODAL_DATA from '@/config/modal.config'
import BaseForm from '@/mviews/components/BaseForm.vue'
import { mapGetters} from 'vuex';
export default {
  components:{
    BaseForm
  },
  data(){
    return {
      fileBase: '',
      type: this.state
    }
  },
  props: {
    state: {
      type: String,
      default: 'login'
    }
  },
created(){
},
mounted(){
},  
watch:{},
computed:{
  reqType() {
    return this.formType === 'userInfo' ? 'putUserInfo' : this.formType 
  },
  formType () {
    return this.nikname ? 'userInfo' : (this.type || this.state)
  },
  btnName() {
    return BTN_NAME[this.formType] || '提交'
  },
  title () {
    return MODAL_DATA[this.formType]?.title
  },
  avatar () {
    return this.fileBase || this.userInfo?.avatar
  },
  nikname() {
    return this.userInfo?.nikname
  },
  ...mapGetters(['userInfo'])
},
methods:{
  gotoRegister() {
    this.type = 'register'
  },
  gotoLogin() {
    this.type = 'login'
  },
  async afterRead(upload) {
    this.fileBase = upload.content
    let formData = new FormData()
    formData.append('file', upload.file)
    try {
      let result = await this.$api({
        type: 'uploadUser', data: formData
      })
      this.fileBase = result.fileURL
    }
    catch(err)
    {
      console.log(err);
    }
  },  
  async onSubmit () {
      let formWrap = this.$refs.userForm;
      let formData = formWrap.$refs.form;
      formData.validate().then(
        async() => {
          if(this.fileBase)
          {
            formWrap.form.avatar = this.fileBase
          }
          try{
            await this.$api({type: this.reqType, data: formWrap.form})
            this.$store.dispatch('getUserInfo')
          }
          catch(err) {
            //重置表单
            formData.resetValidation();
          }
        }
      ).catch(err => {
        console.log(err);
      })
  },
  logout() {
    this.$store.dispatch('logout')
  }
},
}
</script>
<style lang="scss" scoped>
.blogm-user--form {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    padding: 20px 10px;
    background-color: #f5f5f5;
    height: 78vh;
}
  .blogm-user--content {
    width: 100%;
    padding: 20px 0;
    background-color: #fff;
    border-radius: 8px;
    margin-bottom: 50px;
  }
  .blogm-user--title {
    font-size: 20px;
    font-weight: 600;
    text-align: center;
    line-height: 1.8;
  }

  .blogm-user--btn {
    width: 100%;
    >button+button {
      margin-top: 8px;
    }
  }

  .blogm-user--hint {
    text-align: center;
    margin-top: 10px;
  }
</style>